import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-06
 * Time: 22:52
 */
public class Main {
    //自己写的
    public static int[] twoSum(int[] price, int target) {
        int left = 0;
        int right = price.length - 1;
        int arr[] = new int[2];
        while (left < right) {
            if (price[left] + price[right] == target) {
                arr[0] = price[left];
                arr[1] = price[right];
                return arr;
            } else if (price[left] + price[right] < target) {
                left++;
            } else {
                right--;
            }
        }
        return null;
    }

    //老师写的
    public int[] twoSum1(int[] nums,int target){
        int left = 0,right = nums.length-1;
        while (left < right){
            int sum = nums[left] + nums[right];
            if (sum > target){
                right--;
            }else if (sum < target){
                left++;
            }else {
                return new int[]{nums[left],nums[right]};
            }
        }
        return new int[]{0};
    }





    public static void main(String[] args) {
        int arr[] = {3,9,12,15};
        System.out.println(Arrays.toString(twoSum(arr,18)));
    }
}
